package com.aliengen.thingple.driverapp.network.api.models;

import com.aliengen.thingple.driverapp.data.models.TruckTag;
import com.aliengen.thingple.driverapp.network.api.exception.FieldNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class Cylinder implements Serializable {
    public int cylinder_id = 0;

    /**
     * Content stored in the EPC.
     */
    public int gasTypeId = 0;
    public int productId = 0;
    public int facilityCode = 0;
    public int tagStatus = 0;
    public int month = 0;
    public int day = 0;
    public int maintenanceWeek = 0;
    public int bundleId = 0;
    public String bundleReference = "";
    public String huashenNumber = "";

    /**
     * Content retrieved from the database.
     */
    public String pressure;
    public int operationId;
    public Long lastSeenTimestamp;
    public String anomaly;
    public String anomalyStep;

    /**
     * Contextual content
     */
    public TruckTag truckTag;

    public Cylinder() {}

    public Cylinder(String huashenNumber, int operation_id)
    {
        this.huashenNumber = huashenNumber;
        lastSeenTimestamp = new Long(System.currentTimeMillis() / 1000);
        this.operationId = operation_id;
    }

    public Cylinder(JSONObject json) throws FieldNotFoundException {
        if (!(json.has("reference") && json.has("cylinder_id"))) {
            throw new FieldNotFoundException("Missing fields!");
        }

        try {
            this.cylinder_id = (Integer) json.get("cylinder_id");
            this.huashenNumber = (String) json.get("reference");
            if(json.has("pressure")) {
                this.pressure = (String) json.get("pressure");
            }
            this.operationId = (Integer) json.get("operation_id");
            this.anomaly = "";
            this.anomalyStep = "";
        } catch(JSONException e)
        {
            throw new FieldNotFoundException(e);
        }
    }

    public void update(JSONObject json) throws FieldNotFoundException {
        if (!(json.has("reference") && json.has("cylinder_id"))) {
            throw new FieldNotFoundException("Missing fields!");
        }

        try {
            this.cylinder_id = Integer.parseInt((String) json.get("cylinder_id"));
            this.huashenNumber = (String) json.get("reference");
            //this.pressure = json.get("pressure").toString();
            this.pressure = "";
            this.operationId = Integer.parseInt(json.get("operation_id").toString());
            this.anomaly = "";
            this.anomalyStep = "";
        } catch(JSONException e)
        {
            throw new FieldNotFoundException(e);
        }
    }

    @Override
    public String toString()
    {
        return this.huashenNumber;
    }
}
